// pages/succeed/succeed.js
var num = 0//计时
var strH = ''
var strM = ''
var strS = ''
var timer = ''


Page({
  data: {
    uid: '',
    oid: '',
    titleText: '等待抢单',
    animationData: {},
    widht: '',
    height: '',
    remove: '',
    timeText: ''//展示
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    console.log(options)
    var that = this;
    that.setData({
      uid: options.uid,
      oid: options.oid
    })
    wx.setNavigationBarTitle({
      title: that.data.titleText,
    })
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: res.windowHeight + 'px',
          width: res.windowWidth
        })
      }
    })
    clearInterval(timer);
    num = 0;
  },
  move() {
    //时
    strH = this.zeroFill('' + parseInt(num / 3600 % 24), 2)
    //分
    strM = this.zeroFill('' + parseInt(num / 60 % 24), 2)
    //秒
    strS = this.zeroFill('' + parseInt(num % 60), 2)
    //赋值给text内容
    this.setData({
      timeText: strH + ':' + strM + ':' + strS
    })
    //当时间归零停止计时器
    if (num == 1800) {
      clearInterval(timer)
      return
    }
    if (num == 60) {
      wx.navigateTo({
        url: '../pay/pay?oid=' + this.data.oid,
      })
    }
    //每秒递减
    num++;
  },

  zeroFill(str, n) {
    //补零方法，str为数字字符串 n为需要的位数，不够补零
    if (str.length < n) {
      str = '0' + str
    }
    return str
  },
  // 检测输入内容
  remove: function (e) {
    var that = this;
    that.setData({
      remove: e.detail.value
    })
  },
  // 点击跳转到取消订单页面
  clickRemove: function () {
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
    })
    var width = this.data.width;
    this.animation = animation
    this.animation.translate(0, 0).step({ duration: 500 })

    this.animation.translate(-width, 0).step({ duration: 500 })
    this.setData({
      animationData: this.animation.export(),
    })
  },

  // 点击取消订单并停止定时器
  submitRemove: function () {
    var that = this
    wx.request({
      url: "http://1.aiym.applinzi.com/aiyouwai.php",
      data: {
        type: "cancelTheOrder",
        paras: {
          oid: this.data.oid,
          status: this.data.remove
        }
      },
      header: { "Content-Type": "application/json" },
      success: function (res) {
        console.log(res)
        clearInterval(timer);
        var animation = wx.createAnimation({
          duration: 500,
          timingFunction: 'ease',
        })
        var width = that.data.width;
        that.animation = animation
        that.animation.translate(-width - 1, 0).step({ duration: 500 })
        that.animation.translate(-width * 2, 0).step({ duration: 500 })
        that.setData({
          animationData: that.animation.export(),
        })
      }
    })
  },
  onShow: function () {
    this.move()
    //计时开始 后面的1000是毫秒 每1000毫秒跳一次
    timer = setInterval(this.move, 1000);
  }
})